//
//  ClassifyViewController.m
//  DaysComicSugar
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ClassifyViewController.h"
#import "ClassifyCell.h"
#import "Classify22Model.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "ClassifyDetailsViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ClassifyViewController ()
@property(nonatomic, retain)UIScrollView *classifyScrollView;
@property(nonatomic, retain)UIPageControl *classifyPageControl;
@property(nonatomic, retain)UICollectionView *classifyCollectionView;
@property(nonatomic, retain)NSMutableArray *classifyArr;//用来存放model的数组
@property(nonatomic, retain)Classify22Model *classify;
@end

@implementation ClassifyViewController

#pragma mark 初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"漫画分类";
        self.classifyArr = [NSMutableArray array];
    }
    return self;
}

#pragma mark 释放内存
-(void)dealloc
{
    [_classifyScrollView release];
    [_classifyPageControl release];
    [_classifyCollectionView release];
    [_classifyArr release];
    [_classify release];
    [super dealloc];
}

#pragma mark viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createClassifyView];
    [self getData];
}

#pragma mark 创建视图
- (void)createClassifyView
{
    //创建ScrollView
    self.classifyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
    [self.view addSubview:self.classifyScrollView];
    self.classifyScrollView.contentSize = CGSizeMake(WIDTH* 4, HEIGHT / 4);
    self.classifyScrollView.pagingEnabled = YES;
    self.classifyScrollView.delegate = self;
    self.classifyScrollView.backgroundColor = [UIColor greenColor];
    [_classifyScrollView release];
    
    //创建PageControl
    self.classifyPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT / 16 * 3 , WIDTH, HEIGHT / 16)];
    self.classifyPageControl.numberOfPages = 4;
    [self.view addSubview:self.classifyPageControl];
    self.classifyPageControl.pageIndicatorTintColor = [UIColor yellowColor];
    [_classifyPageControl release];
    
    //创建FlowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    //创建CollectionView
    self.classifyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 4 , WIDTH, HEIGHT / 4 * 3 - 110) collectionViewLayout:flowLayout];
    [self.view addSubview:self.classifyCollectionView];
    self.classifyCollectionView.dataSource = self;
    self.classifyCollectionView.delegate = self;
    self.classifyCollectionView.backgroundColor = [UIColor whiteColor];
    [_classifyCollectionView release];
    
    //注册cell
    [self.classifyCollectionView registerClass:[ClassifyCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //导航栏左Button
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(30, 0, 50, 30);
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
}

#pragma mark 协议里面有两个必须实现的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classifyArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    self.classify = self.classifyArr[indexPath.row];
    cell.classifyLabel.text = self.classify.CatalogName;
   NSURL *url = [NSURL URLWithString:self.classify.CatalogOtherURL];
    [cell.classifyImageView sd_setImageWithURL:url];
    return cell;
}

#pragma mark collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyDetailsViewController *classifyDetVC = [[ClassifyDetailsViewController alloc] init];
    self.classify = self.classifyArr[indexPath.row];
    classifyDetVC.tempID = self.classify.CatalogID;
    classifyDetVC.tempTitle = self.classify.CatalogName;
    [self.navigationController pushViewController:classifyDetVC animated:YES];
    [classifyDetVC release];
}


#pragma mark 解析数据
- (void)getData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=catalogs" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
        for (int i = 0; i < arr.count; i++) {
            self.classify = [[Classify22Model alloc] init];
            [self.classify setValuesForKeysWithDictionary:arr[i]];
            [self.classifyArr addObject:self.classify];
        }
        [self.classifyCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
        NSLog(@"%@", error);
    }];
}

#pragma mark 返回点击方法
- (void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
